package pre.liuxl.blog.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import pre.liuxl.blog.entity.Permission;
import pre.liuxl.blog.entity.User;
import pre.liuxl.blog.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by liuxuanlong on 2018/6/16.
 */
public class SecurityInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(SecurityInterceptor.class);
    @Autowired
    UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        logger.info("进入登陆验证");
        HttpSession session = httpServletRequest.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (null == loginUser) {
            logger.info("no login");
            httpServletRequest.getRequestDispatcher("/login").forward(httpServletRequest, httpServletResponse);
            return false;
        } else {

            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        /*logger.info("进行权限验证");

        HttpSession session = httpServletRequest.getSession();
        List<Permission> permissionList = (List<Permission>) session.getAttribute("permission");

        if (null != permissionList) {
            User loginUser = (User) session.getAttribute("loginUser");
            List<Permission> permissionList1 = userService.findUserPermissionByUserId(loginUser.getId());
            session.setAttribute("permission", permissionList1);
        }*/
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
